
/**		
 * 
 *	uk.co.lmfm.guppy.view.components.settings.SettingsView
 *	
 *	@version 1.00 | Jan 12, 2010
 *	@author Justin Windle
 *  
 **/
 
package uk.co.lmfm.guppy.view.components.settings 
{
	import uk.co.lmfm.guppy.model.vo.SettingsVO;
	import uk.co.lmfm.guppy.view.components.core.AbstractView;
	import uk.co.lmfm.guppy.view.components.core.IViewComponent;
	import uk.co.lmfm.guppy.view.components.core.form.InputButtonField;
	import uk.co.lmfm.guppy.view.components.core.form.LabeledFormField;
	import uk.co.lmfm.guppy.view.components.events.PopUpViewEvent;
	import uk.co.lmfm.guppy.view.components.events.SettingsViewEvent;

	import com.bit101.components.PushButton;

	import org.osflash.thunderbolt.Logger;

	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;

	/**
	 * SettingsView
	 */
	public class SettingsView extends AbstractView implements IViewComponent 
	{
		//	----------------------------------------------------------------
		//	PRIVATE MEMBERS
		//	----------------------------------------------------------------

		private var _workspaceField : InputButtonField = new InputButtonField();		private var _libraryField : InputButtonField = new InputButtonField();		private var _eclipseField : InputButtonField = new InputButtonField();
		private var _usernameField : LabeledFormField = new LabeledFormField();		private var _flashIDEField : InputButtonField = new InputButtonField();		private var _flashPlayerField : InputButtonField = new InputButtonField();

		private var _saveButton : PushButton = new PushButton();		private var _autoSetupButton : PushButton = new PushButton();
		private var _autosetupPopup : AutoSetupPopup = new AutoSetupPopup();

		private var _activeField : InputButtonField;
		private var _settings : SettingsVO;
		private var _directory : File = new File();

		//	----------------------------------------------------------------
		//	CONSTRUCTOR
		//	----------------------------------------------------------------

		public function SettingsView()
		{
			initDisplay();
		}

		//	----------------------------------------------------------------
		//	PUBLIC METHODS
		//	----------------------------------------------------------------

		override public function show() : void
		{
			super.show();
		}

		override public function hide() : void
		{
			super.hide();
		}

		public function showAutoSetupMessage() : void
		{
			_autosetupPopup.show();
		}

		//	----------------------------------------------------------------
		//	PROTECTED METHODS
		//	----------------------------------------------------------------

		override protected function resize() : void
		{
			super.resize();
			
			_workspaceField.x = 10;			_libraryField.x = 10;			_eclipseField.x = 10;			_usernameField.x = 10;			_flashIDEField.x = 10;			_flashPlayerField.x = 10;			_saveButton.x = stage.stageWidth - _saveButton.width - 10;
			_autoSetupButton.x = _saveButton.x - _saveButton.width - 10;
			
			graphics.clear();
			graphics.lineStyle(0, 0xCCCCCC);
			
			var p : int = 10;
			var w : int = stage.stageWidth - 20;
			var nextPos : Function = function():int 
			{ 
				return (p += 45) - 45;
			};
			var div : Function = function():void
			{
				p += 10;
				graphics.moveTo(0, p);
				graphics.lineTo(stage.stageWidth, p);
				p += 10;
			};
			
			_usernameField.y = nextPos();
			
			div();
			
			_workspaceField.y = nextPos();
			_libraryField.y = nextPos();			_eclipseField.y = nextPos();
			
			div();
						_flashIDEField.y = _flashPlayerField.y = nextPos();
			
			div();
			
			_saveButton.y = stage.stageHeight - _saveButton.height - 50;
			_autoSetupButton.y = _saveButton.y;
			
			_workspaceField.width = _libraryField.width = _eclipseField.width = w - 140;			_flashIDEField.width = _flashPlayerField.width = (w * 0.5) - 5;
			_usernameField.width = 150;
			
			_flashPlayerField.x = _flashIDEField.x + _flashIDEField.width + 10;
		}

		//	----------------------------------------------------------------
		//	PRIVATE METHODS
		//	----------------------------------------------------------------

		private function initDisplay() : void
		{
			_workspaceField.label.text = "Workspaces Directory (where all of your workspaces live)";
			_workspaceField.button.label = "Browse";
			_workspaceField.button.addEventListener(MouseEvent.CLICK, onButtonClicked);
			
			_libraryField.label.text = "Guppy Library Folder (where Guppy resources will be stored)";
			_libraryField.button.label = "Browse";
			_libraryField.button.addEventListener(MouseEvent.CLICK, onButtonClicked);
			
			_eclipseField.label.text = "Eclipse / FDT Directory (e.g. Applications/Eclipse)";
			_eclipseField.button.label = "Browse";
			_eclipseField.button.addEventListener(MouseEvent.CLICK, onButtonClicked);
			
			_flashIDEField.label.text = "Flash IDE (e.g. Applications/Adobe Flash CS4)";
			_flashIDEField.button.label = "Browse";
			_flashIDEField.button.addEventListener(MouseEvent.CLICK, onButtonClicked);
			
			_flashPlayerField.label.text = "Debug Player (e.g. /Adobe Flash CS4/Players/Debug)";
			_flashPlayerField.button.label = "Browse";
			_flashPlayerField.button.addEventListener(MouseEvent.CLICK, onButtonClicked);
			
			_usernameField.label.text = "User Name";
			
			_saveButton.label = "Save";
			_saveButton.addEventListener(MouseEvent.CLICK, onButtonClicked);
			
			_autoSetupButton.label = "Auto Setup";
			_autoSetupButton.addEventListener(MouseEvent.CLICK, onButtonClicked);
			
			_autosetupPopup.addEventListener(PopUpViewEvent.CONFIRM, onAutosetupCancel);
			
			_workspaceField.button.width = 60;			_libraryField.button.width = 60;			_eclipseField.button.width = 60;			_flashIDEField.button.width = 60;			_flashPlayerField.button.width = 60;
			
			addChild(_workspaceField);
			addChild(_libraryField);			addChild(_eclipseField);			addChild(_usernameField);			addChild(_flashIDEField);			addChild(_flashPlayerField);
			addChild(_saveButton);
			addChild(_autoSetupButton);
			addChild(_autosetupPopup);
		}

		private function browseForDirectory(title : String = null) : void
		{
			try
			{
				_directory = _directory.resolvePath("/").resolvePath(_activeField.input.text);
			}
			catch(error : Error)
			{
				Logger.error("Cannot set directory path: " + error.message);
			}
			
			_directory.addEventListener(Event.SELECT, onDirectorySelected);
			_directory.browseForDirectory(title || "Please select a Directory");
		}

		private function searchForApplication(directory : File, filter : RegExp) : Array
		{
			var files : Array = directory.getDirectoryListing();
			var results : Array = [];
					
			for each (var file : File in files)
			{
				if(file.nativePath.search(filter) > -1)
				{
					results.push(file);
				}
			}
			
			return results;
		}

		//	----------------------------------------------------------------
		//	EVENT HANDLERS
		//	----------------------------------------------------------------

		private function onButtonClicked(event : MouseEvent) : void
		{
			switch(event.currentTarget)
			{
				case _workspaceField.button : 
				
					_activeField = _workspaceField;
					browseForDirectory("Select your Workspaces Directory");
				
					break;
					
				case _libraryField.button : 
				
					_activeField = _libraryField;
					browseForDirectory("Select your Guppy Library Directory");
				
					break;
					
				case _eclipseField.button : 
				
					_activeField = _eclipseField;
					browseForDirectory("Select your Eclipse / FDT Directory");
				
					break;
					
				case _flashIDEField.button : 
				
					_activeField = _flashIDEField;
					browseForDirectory("Select your Flash IDE Directory");
				
					break;
					
				case _flashPlayerField.button : 
				
					_activeField = _flashPlayerField;
					browseForDirectory("Select your Flash Debug Player Directory");
				
					break;
					
				case _autoSetupButton : 
				
					dispatchEvent(new SettingsViewEvent(SettingsViewEvent.AUTO_SETUP_REQUESTED));
				
					break;
					
				case _saveButton : 
				
					_settings.workspaceLocation = _workspaceField.inputText.text;
					_settings.libraryPath = _libraryField.inputText.text;
					
					dispatchEvent(new SettingsViewEvent(SettingsViewEvent.SAVE_SELECTED));
				
					break;
			}
		}

		private function onDirectorySelected(event : Event) : void
		{
			_directory.removeEventListener(Event.SELECT, onDirectorySelected);
			
			switch(_activeField)
			{
				case _workspaceField : 
				
					_settings.workspaceLocation = _workspaceField.inputText.text = _directory.nativePath;
				
					break;
					
				case _libraryField : 
				
					_settings.libraryPath = _libraryField.inputText.text = _directory.nativePath;
				
					break;
					
				case _flashIDEField : 
				
					_settings.flashIDE = _flashIDEField.inputText.text = searchForApplication(_directory, /flash cs([0-9]).app$/gi)[0].nativePath;
					
					break;
					
				case _flashPlayerField : 
				
					_settings.flashPlayer = _flashPlayerField.inputText.text = searchForApplication(_directory, /flash\s?player.app$/gi)[0].nativePath;

					break;
					
				case _eclipseField : 
				
					_settings.eclipsePath = _eclipseField.inputText.text = _directory.nativePath;
				
					break;
			}
		}

		private function onAutosetupCancel(event : PopUpViewEvent) : void
		{
			dispatchEvent(new SettingsViewEvent(SettingsViewEvent.AUTO_SETUP_CANCELLED));
		}

		//	----------------------------------------------------------------
		//	PUBLIC ACCESSORS
		//	----------------------------------------------------------------

		public function get settings() : SettingsVO
		{
			// Carefult not to cause stack overflow by using the settings member mutators.

			_settings.workspaceLocation = _workspaceField.inputText.text;
			_settings.libraryPath = _libraryField.inputText.text;
			_settings.eclipsePath = _eclipseField.inputText.text;			_settings.flashIDE = _flashIDEField.inputText.text;			_settings.flashPlayer = _flashPlayerField.inputText.text;
			_settings.username = _usernameField.value;
			
			return _settings;
		}

		public function set settings(value : SettingsVO) : void
		{
			_settings = value;
			
			_workspaceField.inputText.text = _settings.workspaceLocation;
			_libraryField.inputText.text = _settings.libraryPath;			_eclipseField.inputText.text = _settings.eclipsePath;			_flashIDEField.inputText.text = _settings.flashIDE;			_flashPlayerField.inputText.text = _settings.flashPlayer;			_usernameField.input.text = _settings.username;
			
			var n : Number = 0.0;
			var s : Number = 1 / 6;
			
			if(_settings.username != '') n += s;
			if(_settings.libraryPath != '') n += s;
			if(_settings.workspaceLocation != '') n += s;
			if(_settings.eclipsePath != '') n += s;
			if(_settings.flashIDE != '') n += s;
			if(_settings.flashPlayer != '') n += s;
			
			_autosetupPopup.progress = n;
		}
	}
}
